package com.sf.dlb.controller;


import com.sf.dlb.bean.UserRquestBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.service.AppConfigService;
import com.sf.dlb.service.AppVersionService;
import com.sf.dlb.config.Global;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * 系统设置相关
 * Created by jing.tan on 2018/12/10.
 */
@RestController
@RequestMapping(value = "/v1/sys")
public class SystemController {
    private static Logger logger = LoggerFactory.getLogger(SystemController.class);

    @Resource
    private Global global;
    @Autowired
    private AppVersionService appVersionService;
    @Autowired
    private AppConfigService appConfigService;
    /**
     * 获取项目首页url
     *
     * @return
     */
    @GetMapping(value = "/getIndex")
    @ResponseBody
    public Object getIndex() {
        Map<String,String> map=new HashMap<>();
        map.put("url",global.project_index_path);
        ResultVO resultVO=ResultVOUtil.success(map);
        logger.info("getIndex response json " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 获取APP最新版本
     * @return
     */
    @GetMapping(value = "/appVersion/{systemType}")
    @ResponseBody
    public Object appVersion(@PathVariable("systemType") String systemType) {
        if(StringUtils.isBlank(systemType)){
            return ResultVOUtil.error(ResponseConstant.ERROR,"系统类型不能为空");
        }
        ResultVO resultVO=appVersionService.findBySystemType(systemType);
        logger.info("appDownLoad response json " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 注册(公测)
     */
    @PostMapping(value = "/registerPub")
    @ResponseBody
    public Object registerPub(@RequestBody UserRquestBean userRquestBean) {
        //判断参数是否合法
        if (null==userRquestBean) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("registerPub request json === " + GsonUtils.getInstance().toJson(userRquestBean));
        if(StringUtils.isEmpty(userRquestBean.getMobile())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "手机号不能为空");
        }
        if(StringUtils.isEmpty(userRquestBean.getValidationCode())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "验证码不能为空");
        }
        if(StringUtils.isBlank(userRquestBean.getInviteCode())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "邀请码不能为空");
        }
        ResultVO resultVO=ResultVOUtil.error(ResponseConstant.ERROR, "该版本已停止使用");
        logger.info("registerPub response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * APP配置
     */
    @GetMapping(value = "/app/config")
    @ResponseBody
    public Object appConfig() {
        logger.info("appConfig request start === ");
        ResultVO resultVO=appConfigService.appConfig();
        logger.info("appConfig response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }

}
